package mapEditor.view.resItemEdit
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	
	import iso.IsoDisplayObject;
	import iso.IsoPoint;
	
	import mapEditor.data.AssetTemplateInfo;
	import mapEditor.vo.AssetVO;
	import mapEditor.vo.ResItemItemVO;
	import mapEditor.vo.ResItemVO;
	
	import utils.ApplicationDomainUtil;
	import utils.DisplayUtil;

	public class ResItemEditItemView extends IsoDisplayObject
	{
		private var _resItemItemVO:ResItemItemVO;
		private var _assetVO:AssetVO;
		private var _displayObject:DisplayObject;
		private var _mousePoint:Point=new Point();
		
		public function ResItemEditItemView(resItemItemVO:ResItemItemVO)
		{
			_resItemItemVO=resItemItemVO;
			_resItemItemVO.addEventListener(Event.CHANGE, __onResItemItemVOChange);
			_assetVO=AssetTemplateInfo.instance.getAssetVOByID(_resItemItemVO.assetID);
			loadAsset();
		}
		
		private function __onResItemItemVOChange(evt:Event):void
		{
			visible=_resItemItemVO.visible;
			x=_resItemItemVO.x;
			y=_resItemItemVO.y;
			z=_resItemItemVO.z;
			graphics.clear();
			if(_resItemItemVO.selected)
			{
				var rec:Rectangle=getBounds(_displayObject);
				graphics.lineStyle(1, 0x0066FF);
				graphics.drawRect(rec.x+_assetVO.registerX, rec.y+_assetVO.registerY, rec.width, rec.height);
				graphics.endFill();
			}
		}
		
		private function loadAsset():void
		{
			var obj:*=ApplicationDomainUtil.getDefinitionObject(_assetVO.className);
			if(obj is BitmapData)
			{
				_displayObject=new Bitmap(obj);
			}
			else if(obj is MovieClip)
			{
				_displayObject=obj as DisplayObject;
			}
			_displayObject.x=_assetVO.registerX;
			_displayObject.y=_assetVO.registerY;
			_displayObject.z=_assetVO.registerZ;
			addChild(_displayObject);
			x=_resItemItemVO.x;
			y=_resItemItemVO.y;
			z=_resItemItemVO.z;
		}
		
		private function clearAsset():void
		{
			_assetVO=null;
			DisplayUtil.dispose(_displayObject);
			_displayObject=null;
		}
		
		override public function set position(value:IsoPoint):void
		{
			super.position=value;
			_resItemItemVO.x=value.x;
			_resItemItemVO.y=value.y;
			_resItemItemVO.z=value.z;
			_resItemItemVO.change();
		}
		
		override public function dispose():void
		{
			super.dispose();
			clearAsset();
			if(_resItemItemVO)
			{
				_resItemItemVO.removeEventListener(Event.CHANGE, __onResItemItemVOChange);
				_resItemItemVO=null;	
			}
			_mousePoint=null;
			if(parent)parent.removeChild(this);
		}

		override public function set x(value:Number):void
		{
			super.x=value;
			_resItemItemVO.x=value;
		}

		override public function set y(value:Number):void
		{
			super.y=value;
			_resItemItemVO.y=value;
		}

		override public function set z(value:Number):void
		{
			super.z=value;
			_resItemItemVO.z=value;
		}
		
		public function get resItemItemVO():ResItemItemVO
		{
			return _resItemItemVO;
		}

		public function get mousePoint():Point
		{
			return _mousePoint;
		}
	}
}